import { login, logout, getInfo, getUserList, addUser, delUser, updateUser, updateUserMyself } from 'api/user'
import { getToken, setToken, removeToken } from 'common/utils/'

const user = {
  state: {
    token: getToken(),
    userId: '',
    username: '',
    avatar: '',
    roles: [],
    list: [],
    total: 0,
    others: {}
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_INFO: (state, info) => {
      state.userId = info._id
      state.username = info.username
      state.name = info.name
      state.avatar = info.avatar
      state.roles = info.roles
      state.others = info.others
    },
    // SET_ROLES: (state, roles) => {
    //   state.roles = roles
    // },
    SET_USERLIST(state, userlist) {
      state.list = userlist.list
      state.total = userlist.total
    },
    SET_USER_NAME: (state, username) => {
      state.username = username
    },
    SET_AVATAR(state, imgUrl) {
      state.avatar = imgUrl
    }
  },
  actions: {
    // 登录
    Login({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        login(userInfo.username, userInfo.password).then(response => {
          const data = response.data
          setToken(data.token)
          commit('SET_TOKEN', data.token)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo(state.token).then(response => {
          commit('SET_INFO', response.data)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 获取管理员列表
    GetUserList({ commit }, info) {
      return new Promise((resolve, reject) => {
        getUserList(info).then(response => {
          const data = response.data
          commit('SET_USERLIST', data)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 增加管理员
    AddUser({ commit }, info) {
      return new Promise((resolve, reject) => {
        addUser(info).then(response => {
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 删除管理员
    DelUser({ commit }, id) {
      return new Promise((resolve, reject) => {
        delUser(id).then(response => {
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    //  超级管理员 更新 其他管理员 的账户名 密码 头像
    UpdateUser({ commit }, info) {
      return new Promise((resolve, reject) => {
        updateUser(info).then(response => {
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 管理员更新自己的账户名 密码 头像
    UpdateUserMyself({ commit }, info) {
      const id = info.id
      delete info.id
      return new Promise((resolve, reject) => {
        updateUserMyself(id, info).then(response => {
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 登出
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          // commit('SET_ROLES', [])
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    }
  }
}

export default user
